<template>
	<el-container>
		<el-header>
			<el-page-header :content="flowData.metadata.description" @back="goBack"></el-page-header>
			<div class="do">
				<el-button type="primary" @click="exportJson">export JSON</el-button>
				<el-button type="primary" @click="test">test</el-button>
				<el-button type="primary" @click="test">发布</el-button>
			</div>
		</el-header>
		<el-main>
			<sc-workflow ref="flow" v-model="flowData"></sc-workflow>
		</el-main>
	</el-container>
</template>

<script>
	import scWorkflow from '@/components/scWorkflow'

	export default {
		name: 'workflow',
		components: {
			scWorkflow
		},
		data() {
			return {
				flowData: {
					id: "workflow_xc2fsdf",
					name: "开始",
					type: "start",
					metadata: {
						description: "打标签工作流",
					},
					childNode: {
						name: "代码执行",
						type: "code",
						metadata: {
							description: "编写代码，处理输入变量来生成返回值",
						},
						properties: {
							code: `// 在这里，您可以通过 ‘params’ 获取节点中的输入变量，并通过 'ret' 输出结果
// 'params' 和 'ret' 已经被正确地注入到环境中
// 下面是一个示例，获取节点输入中参数名为‘username’和‘password’的值：
// const { username, password } = params;
// 下面是一个示例，输出一个包含多种数据类型的 'ret' 对象：
// const ret = { "name": ‘小明’, "hobbies": [“看书”, “旅游”] };
// $.env 是环境变量 比如 $.env.username

/**
 * @function main 入口函数
 * @param {string} username 用户名 - 请输入用户名
 * @param {string} password 密码 - 请输入密码
 * @returns {string} key0
 * @returns {array<string>} key1
 * @returns {object} key2
 * @returns {string} key2.key21
 */
const main = async (params) => {

	var { username, password } = params;

	// 构建输出对象
	const ret = {
		"key0": username + "|" + password, // 拼接字符串
		"key1": ["hello", "world"], // 输出一个数组
		"key2": { // 输出一个Object
			"key21": "hi"
		},
	};

	return ret;

}




`,
							settingOnError: {
								switch: true,
								dataOnErr: {

								}
							},
						},
						"childNode": {
							"id": "switch_sdfs12ss",
							"name": "条件分支1",
							"type": "switch",
							"conditionNodes": [{
									"name": "长期",
									"type": "case",
									"properties": {

									},
									"childNode": {
										"name": "钉钉机器人",
										"type": "action",
										"appId": "dingtalk.js",
										"appName": "钉钉机器人",
										"eventName": "发送钉钉消息",
										"properties": {},
									}
								},
								{
									"name": "短期",
									"type": "case",
									"properties": {},
									"childNode": {
										"name": "钉钉机器人",
										"type": "action",
										"childNode": {
											"name": "条件分支2",
											"type": "switch",
											"conditionNodes": [{
													"name": "长期",
													"type": "case",
													"priorityLevel": 1,
													"conditionMode": 1,
													"conditionList": [{
														"label": "请假天数",
														"field": "day",
														"operator": ">",
														"value": "7"
													}],
													"childNode": {
														"name": "钉钉机器人",
														"type": "action",
													}
												},
												{
													"name": "短期",
													"type": "case",
													"priorityLevel": 2,
													"conditionMode": 1,
													"conditionList": [],
													"childNode": {
														"name": "钉钉机器人",
														"type": "action",
													}
												},
												{
													name: "默认条件",
													type: "default",
													priorityLevel: 0,
													conditionMode: 1,
													conditionList: []
												}
											]
										}
									}
								},
								{
									name: "默认条件",
									type: "default",
									condition: {

									}
								}
							],
							"childNode": {
								"name": "咨询",
								"type": "action",
							}
						}

					}
				}
			}
		},
		mounted() {

		},
		methods: {
			test() {
				this.$refs.flow.updateIndex()

				return;

				this.$refs.flow.forNode(this.data.nodeConfig, null, (item) => {

					var nextId = ""
					var temp = item
					var i = 0

					if (item.type) {

						if (!item.childNode || (item.childNode && Object.keys(item.childNode).length == 0)) {

							try {
								for (i = 0; i < 100; i++) {
									if (temp) {
										temp = temp.parent
										if (temp && temp.childNode) {
											if (temp.childNode.index > item.index) {
												if (temp.childNode.index) {
													nextId = temp.childNode.index;
													break;
												}
											}
										}
										if (temp && temp.type == "for") {
											nextId = temp.index;
											break;
										}
									}
								}
							} catch (e) {
								console.log("test", e)
							}

							if (!nextId) {

								temp = item

								try {
									for (i = 0; i < 100; i++) {
										if (temp) {
											temp = temp.parent
											if (temp && temp.childNode) {
												if (temp.childNode.conditionNodes && temp.childNode.conditionNodes[
														0]
													.index > item.index) {
													nextId = temp.childNode.conditionNodes[0].index;
													break;
												}
											}
										}
									}
								} catch (e) {
									console.log("test", e)
								}

							}

							if (nextId) {
								item.id = item.index + "-" + nextId
							}

						}

					}
				})

			},
			goBack() {
				this.$router.go(-1)
			},
			exportJson() {

				console.error("data", this.data)

				return;
				var nodes = [];
				var edges = [];
				var i = 1;
				this.$refs.flow.forNode(this.data.nodeConfig, null, (item, parent) => {
					item.parent = parent
					if (item.type != 'stop') {
						item.id = item.type == 'switch' ? item.parent.id : i++;
						if (item.type != 'switch' && item.type != 'stop') {
							nodes.push(item)
						}
					}
				})
				this.$refs.flow.forNode(this.data.nodeConfig, null, (item) => {
					if (!item.childNode && !item.conditionNodes) {
						var nextId = ""
						try {
							var temp = item
							for (var i = 0; i < 100; i++) {
								if (temp) {
									temp = temp.parent
									if (temp && temp.childNode && temp.childNode.id > item.id) {
										if (temp.childNode.id) {
											nextId = temp.childNode.id;
											break;
										}
									}
								}
							}
						} catch (e) {
							console.log("test", e)
						}

						if (nextId) {
							edges.push({
								from: item.id,
								to: nextId
							})
							item.name = item.id + "-" + nextId
						} else {
							item.name = item.id
						}
					} else {
						item.name = item.id
					}
				})
				this.$refs.flow.forNode(this.data.nodeConfig, null, (item, parent) => {
					if (parent) {
						if (parent.id != item.id && item.id && parent.id) {
							if (parent.type != 'switch' || item.type != 'action') {
								edges.push({
									from: parent.id,
									to: item.id
								})
							}
						}
					}
				})
				console.error("nodes & edges: ", nodes, edges)
				this.$message("返回值请查看F12控制台console.log()")
				console.log(this.data)
			}
		}
	}
</script>

<style></style>
